package com.qumitech.visualnovel.controller.rest;

import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.controller.vo.block.BlockApplicationConfigVO;
import com.qumitech.visualnovel.controller.vo.block.BlockPlanConfigVO;
import com.qumitech.visualnovel.pubdef.dto.blockPosition.BlockPositionDTO;
import com.qumitech.visualnovel.service.block.BlockService;
import com.google.common.collect.ImmutableMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.transform.Result;

/**
 * @Author :  ZhChNing
 * @Descriptiom :
 * @Date Created in 2019/3/11 9:46
 */

@RestController
@RequestMapping(value = "/api/block")
public class BlockController {

    @Autowired
    private BlockService blockService;

    @GetMapping(value = "/plan")
    public BaseRsp getBlockPlan(@RequestParam(value = "offset", defaultValue = "0") Integer offset,
                                @RequestParam(value = "limit", defaultValue = "20") Integer limit,
                                @RequestParam(value = "block_type", required = false) Byte blockType,
                                @RequestParam(value = "plan_name", required = false) String planName,
                                @RequestParam(value = "plan_id", required = false) Integer planId
    ) {
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("block_plans", blockService.listBlockPlanConfigVO(offset,limit,blockType,planName,planId)));
    }

    @GetMapping(value = "/plan/{planid}")
    public BaseRsp getBlockPlanByPlanId(@PathVariable(name = "planid") Integer planId) {
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("block_plan", blockService.listBlockPlanConfigVO(planId)));
    }

    @PostMapping("/plan")
    public BaseRsp addBlockPlanByPlanId(@RequestBody BlockPlanConfigVO blockPlanConfigVO) {
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("plan_id", blockService.addBlockPlanConfigVO(blockPlanConfigVO)));
    }

    @PutMapping(value = "/plan/{planid}")
    public BaseRsp updateBlockPlan(@PathVariable(name = "planid") Integer planId, @RequestBody BlockPlanConfigVO blockPlanConfigVO) {
        blockPlanConfigVO.setPlanId(planId);
        blockService.updateBlockPlanConfigVO(blockPlanConfigVO);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @DeleteMapping(value = "/plan/{planid}")
    public BaseRsp removeBlockPlan(@PathVariable(name = "planid") Integer planId) {
        blockService.removeBlockPlanConfigVO(planId);
        return new BaseRsp(ResultEnum.SUCCESS);
    }


    @GetMapping(value = "/appliction")
    public BaseRsp getBlockApplication(@RequestParam(value = "offset", defaultValue = "0") Integer offset,
                                       @RequestParam(value = "limit", defaultValue = "20") Integer limit,
                                       @RequestParam(value = "channel" ,required = false) String channel,
                                       @RequestParam(value = "version"  ,required = false) String version,
                                       @RequestParam(value = "device_type" ,required = false) Integer deviceType,
                                       @RequestParam(value = "package_name",required = false) String packageName,
                                       @RequestParam(value = "application_name", required = false) String applicationName) {
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("block_applications", blockService.listBlockApplicationConfigVO(offset,limit,channel,version,deviceType,packageName,applicationName)));
    }

    @PostMapping("/appliction")
    public BaseRsp addBlockApplication(@RequestBody BlockApplicationConfigVO blockApplicationConfigVO) {

        blockService.addBlockApplicationConfigVO(blockApplicationConfigVO);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @PutMapping(value = "/appliction/{applicationid}")
    public BaseRsp updateBlockApplication(@PathVariable(name = "applicationid") Integer applicationId, @RequestBody BlockApplicationConfigVO blockApplicationConfigVO) {
        blockApplicationConfigVO.setApplicationId(applicationId);
        blockService.updateBlockApplicationConfigVO(blockApplicationConfigVO);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @DeleteMapping(value = "/appliction/{applicationid}")
    public BaseRsp removeBlockApplication(@PathVariable(name = "applicationid") Integer applicationId) {
        blockService.removeBlockApplicationConfigVO(applicationId);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @GetMapping(value = "/appinfo")
    public BaseRsp getAppInfo() {
        return new BaseRsp(ResultEnum.SUCCESS, blockService.getAppInfo());
    }

    @PostMapping("/position/config")
    public BaseRsp addPositionConfig(@RequestBody BlockPositionDTO dto){
        blockService.addBlockPosition(dto);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @PutMapping("/position/config")
    public BaseRsp updatePositionConfig(@RequestBody BlockPositionDTO dto){
        blockService.updateBlockPosition(dto);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @GetMapping("/position/config")
    public BaseRsp getPositionCfg(@RequestParam(name = "view_type", required = false) Byte viewType){
        return new BaseRsp(ResultEnum.SUCCESS, blockService.getPositionInfo(viewType));
    }
}
